#include "StandardWorldBuilder.h"
#include "BitmapParser.h"
#include "Ennemy.h"
#include "MovableManager.h"

void StandardWorldBuilder::buildTextures()
{
	sf::Image terrain;
	if (!terrain.loadFromFile("terrain.png"))
	{
		cout<<"erreur de chargement de la texture terrain.png"<<endl;
	}

	IntRect carre(0,0,16,16);
	_world->textures[stone].loadFromImage(terrain,carre);
	carre.left=16;
	_world->textures[stone2].loadFromImage(terrain,carre);
	carre.left=32;
	_world->textures[dirt].loadFromImage(terrain,carre);
	carre.left=48;
	_world->textures[grass].loadFromImage(terrain,carre);
	carre.left=64;
	_world->textures[wood].loadFromImage(terrain,carre);
	carre.left=112;
	_world->textures[brick].loadFromImage(terrain,carre);
	carre.left=192;
	carre.top=16;
	_world->textures[mushroom].loadFromImage(terrain,carre);
	carre.left=208;
	_world->textures[good_mushroom].loadFromImage(terrain,carre);
	carre.left=16;
	carre.top=80;
	_world->textures[exit_door].loadFromImage(terrain,carre);
	carre.left=64;
	carre.top=32;
	_world->textures[moss_stone].loadFromImage(terrain,carre);
	carre.left=16;
	carre.top=16;
	_world->textures[gravel].loadFromImage(terrain,carre);
}

void StandardWorldBuilder::buildLevel(int i)
{

	BitmapParser parser;
	parser.openFile(intToLevelFile(i));
	for(unsigned int i=0; i<parser.getBmp()->GetWidth(); i++)
	{
		for(unsigned int j=0; j<parser.getBmp()->GetHeight(); j++)
		{
			Gdiplus::Color pixelColor;
			parser.getBmp()->GetPixel(i, j, &pixelColor);

			unsigned int red=pixelColor.GetR();
			unsigned int green=pixelColor.GetG();
			unsigned int blue=pixelColor.GetB();
			if(red==255 && green==255 && blue==255){}
			else
			{
			if(red==128 && green==64 && blue==0){ _world->loadTile(i,j,dirt);}
			else if(red==0 && green==128 && blue==0){ _world->loadTile(i,j,grass);}
			else if(red==255 && green==127 && blue==0){ _world->loadTile(i,j,wood);}
			else if(red==200 && green==200 && blue==200){ _world->loadTile(i,j,stone);}
			else if(red==200 && green==0 && blue==0){ _world->loadTile(i,j,brick);}
			else if(red==0 && green==100 && blue==0){ _world->loadTile(i,j,moss_stone);}
			else if(red==100 && green==100 && blue==0){ _world->loadTile(i,j,gravel);}
			else if(red==255 && green==255 && blue==0){ _world->loadItem(i, j, mushroom);}
			else if(red==0 && green==255 && blue==255){ _world->loadItem(i, j, good_mushroom);}
			else if(red==100 && green==100 && blue==100){ _world->loadItem(i, j, exit_door); _world->setEndPosition(Vector2f(i*40,j*40));}
			else if(red==0 && green==0 && blue==0){ _world->setStartingPosition(Vector2f(i*40,j*40));}
			else if(red==0 && green==255 && blue==0){ _world->_MovableManager->addMovable(new Ennemy(Vector2f(36,36),sf::Color::Blue,Vector2f(60,0),Vector2f(i*40,j*40),100, _world->_MovableManager->_animationHandlerFactory));}
			}
		}	
	}
}
void StandardWorldBuilder::buildGravity()
{
	_world->setGravity(500);
}

WCHAR* StandardWorldBuilder::intToLevelFile(int i)
{
	switch (i)
	{
	case 1: return L"level_1.bmp";
	case 2: return L"level_2.bmp";
	case 3: return L"level_3.bmp";
	default: return L"level_1.bmp";
	}
}